﻿namespace calendar_generator
{
	using System;
	using System.Collections.Generic;
	using System.Globalization;
	using System.Linq;
	using System.Text;

	class HtmlRowsGenerator
	{
		private int type;

		public string RowHtml { get; private set; }
		private List<DateTime> monthDates;
		private List<DateTime> halfMonthDates;
		private List<DateTime> newMoons;

		private string GetDayHtml(DateTime date)
		{
			StringBuilder ret = new StringBuilder();

			// Check for month/season concurrence
			string flag = string.Empty;
			int day = 0;

			// Check moon dates
			if(this.newMoons.Contains(date))
			{
				int month = date.Month - 1;
				if (month == 0)
				{ month = 12; }

				ret.Append("<strong>");

				ret.Append(month);

				if (this.type == 0)
				{ ret.Append("м"); }
				else
				{ ret.Append("*"); }

				ret.Append("</strong>");

				day = 1;
			}

			if (monthDates.Contains(date))
			{
				if (this.type == 0)
				{ flag = "*"; }
			}
			else if (halfMonthDates.Contains(date))
			{
				flag = "<strong>**</strong>";
				if (this.type != 0)
				{ day = 1; }
			}

			if (day == 0)
			{
				// Find previous moth start
				DateTime dt = this.newMoons.FindLast(m => m <= date);
				TimeSpan span = date - dt;
				ret.Append(span.TotalDays + 1);
			}

			if (!string.IsNullOrEmpty(flag))
			{
				ret.Append("<div>");
				ret.Append(flag);
				ret.Append("</div>");
			}

			return ret.ToString();
		}

		public HtmlRowsGenerator(int year, int type = 0)
		{
			this.type = type;
			StringBuilder html = new StringBuilder();
			DateTime date;

			// Init class properties
			MilestonesData md = new MilestonesData(year);
			this.monthDates = md.GetMonthDates(0);
			this.halfMonthDates = md.GetMonthDates(1);
			this.newMoons = NewMoons.GetNewMoonsForYear(year - 1).Where(d => d >= new DateTime(year - 1, 12, 1)).ToList();
			this.newMoons.AddRange(NewMoons.GetNewMoonsForYear(year).Where(d => d.Year == year).ToList());

			for (int i = 0; i < 7; i += 6)
			{
				for (int day = 1; day <= 31; day++)
				{
					html.Append("\t\t\t\t<tr>" + Environment.NewLine + "\t\t\t\t\t");
					for (int month = 1; month <= 6; month++)
					{
						try { date = new DateTime(year, month + i, day); }
						catch { date = DateTime.MinValue; }
						if (date != DateTime.MinValue)
						{
							if (month == 1)
							{
								html.Append("<td>");
								html.Append(date.Day.ToString());
								html.Append("</td>");
							}

							Binominal bi = new Binominal(date);
							//html.Append(bi.TdTriad);
							html.Append("<td>");
							html.Append(date.ToString("ddd", new CultureInfo("bg-BG")).Substring(0, 2));
							html.Append("</td><td>");
							html.Append(GetDayHtml(date));
							html.Append("</td><td>");
							if (this.type == 0)
							{ html.Append(bi.BgName); }
							else
							{
								html.Append("<em>");
								//html.Append("<strong>");
								html.Append(bi.BgAbr);
								//html.Append("</strong>");
								html.Append("</em>");
							}
							html.Append("</td>");
						}
						else
						{
							//html.Append("<td colspan=\"3\"></td>");
							html.Append("<td></td><td></td><td></td>");
						}
					}
					html.Append(Environment.NewLine + "\t\t\t\t</tr>");
					html.Append(Environment.NewLine);
				}
				html.Append(Environment.NewLine);
				html.Append("<!-- ================================================================ -->");
				html.Append(Environment.NewLine);
			}

			this.RowHtml = html.ToString();
		}
	}
}
